package com.escafoide.smm;

import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

public class Config extends Activity {
	EditText precio;
	DatePicker date;
	Button aceptar;
	Spinner objetivo;
	AlertDialog.Builder alert;
	Context mContext;
	SharedPreferences sp;
	SharedPreferences.Editor spe;
	Intent i;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);
		initViews();
		initOnClick();
	}

	private void initViews() {
		precio = (EditText) findViewById(R.id.editText1);
		date = (DatePicker) findViewById(R.id.datePicker1);
		aceptar = (Button) findViewById(R.id.aceptar_config);
		objetivo = (Spinner) findViewById(R.id.spinner1);
		mContext = this;
		sp = mContext.getSharedPreferences("SMM",
				Context.MODE_PRIVATE);
		spe = sp.edit();
		i = getIntent();
	}
	
	private void initOnClick() {
		aceptar.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				alert = new AlertDialog.Builder(mContext);

				GregorianCalendar grc = new GregorianCalendar(date.getYear(), date.getMonth(), date.getDayOfMonth(), 23, 59, 59);
				Date date_fin = new Date(grc.getTimeInMillis());
				Date now = new Date();
				
				if (precio.getText().toString().equals("")){
					alert.setTitle("Falten dades");
					alert.setMessage("Has d'introduir els euros que vols estalviar").setPositiveButton("Acceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).show();
				} else if (date_fin.before(now)) {
					alert.setTitle("Data Incorrecta");
					alert.setMessage("Has de seleccionar una data de finalitzaci� del pla d'estalvis posterior a l'actual").setPositiveButton("Acceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).show();
				} else if (objetivo.getSelectedItem().toString().equals("Objectiu")) {
					alert.setTitle("Falten dades");
					alert.setMessage("Has de seleccionar un objectiu").setPositiveButton("Acceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).show();
				} else {
					spe.putLong("fecha", date_fin.getTime());
					spe.putString("euros", precio.getText().toString());
					spe.putString("objetivo", objetivo.getSelectedItem().toString());
					spe.commit();
					if (i.getStringExtra("from") != null){
						if (i.getStringExtra("from").equals("Login")){
							Intent j = new Intent(mContext, graphs.class);
							startActivity(j);
						}
					}
					finish();
				}
			}
		});
		
	}

}
